package com.fastloan.jn.mgt.customermanager.task.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customermanager.task.dao.JnManagerTaskEntryMapper;
import com.fastloan.jn.mgt.customermanager.task.model.JnManagerTaskEntry;
import com.fastloan.jn.mgt.customermanager.task.model.vo.UsetTask;
import com.fastloan.jn.mgt.customermanager.task.service.JnCustomerManagerTaskService;
import com.fastloan.jn.mgt.customermanager.task.model.JnCustomerManagerTask;
import com.fastloan.jn.utils.other.ObjUtil;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gelon
 *         createTime 2017/10/29
 */
@org.springframework.stereotype.Service
public class JnCustomerManagerTaskServiceImpl extends BasicServiceImpl<JnCustomerManagerTask> implements JnCustomerManagerTaskService {
    @org.springframework.beans.factory.annotation.Autowired
    private com.fastloan.jn.mgt.customermanager.task.dao.JnCustomerManagerTaskMapper jnCustomerManagerTaskMapper;

    @Autowired
    private JnManagerTaskEntryMapper taskEntryMapper;


    @Override
    @DataTable(name = "userTasks")
    public List<UsetTask> userTasks(Map<String, Object> map) {
        return jnCustomerManagerTaskMapper.userTasks(map);
    }

    @Override
    public JnCustomerManagerTask selectInfo(Map<String, Object> map) {
        return jnCustomerManagerTaskMapper.selectInfo(map);
    }

    @Override
    @Transactional
    public void saveUpdate(JnCustomerManagerTask managerTask, List<JnManagerTaskEntry> jnManagerTaskEntries) {
        if(ObjUtil.isEmpty(managerTask.getId())){
            managerTask.setCreateTime(new Date());
            managerTask.setCreater("1");
            managerTask.setStatus(0);
            managerTask.setTaskDate(new Date());
            insertSelective(managerTask);
            jnManagerTaskEntries.forEach(jnManagerTaskEntry -> jnManagerTaskEntry.setTaskId(managerTask.getId()));
            jnManagerTaskEntries.forEach(jnManagerTaskEntry -> taskEntryMapper.insertSelective(jnManagerTaskEntry));
        }else{
            updateByPrimaryKeySelective(managerTask);
            jnManagerTaskEntries.forEach(jnManagerTaskEntry -> taskEntryMapper.updateByPrimaryKeySelective(jnManagerTaskEntry));
        }
    }

}
